package hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.providers;

import hu.bme.mit.inf.eclipse.v37zen.awesome.AwesomePackage;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.AwesomeEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.BottomBoxEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.DesktopEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.LeftBoxEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.MenuEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.MenuitemEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.MenuitemSubmenuEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.RightBoxEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.edit.parts.TopBoxEditPart;
import hu.bme.mit.inf.eclipse.v37zen.awesome.diagram.part.AwesomeDiagramEditorPlugin;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

/**
 * @generated
 */
public class AwesomeElementTypes {

	/**
	 * @generated
	 */
	private AwesomeElementTypes() {
	}

	/**
	 * @generated
	 */
	private static Map<IElementType, ENamedElement> elements;

	/**
	 * @generated
	 */
	private static ImageRegistry imageRegistry;

	/**
	 * @generated
	 */
	private static Set<IElementType> KNOWN_ELEMENT_TYPES;

	/**
	 * @generated
	 */
	public static final IElementType Awesome_1000 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.Awesome_1000"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Desktop_2001 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.Desktop_2001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Menu_3001 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.Menu_3001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Menuitem_3002 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.Menuitem_3002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType TopBox_3003 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.TopBox_3003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType BottomBox_3004 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.BottomBox_3004"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType LeftBox_3005 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.LeftBox_3005"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType RightBox_3006 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.RightBox_3006"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType MenuitemSubmenu_4002 = getElementType("hu.bme.mit.inf.eclipse.v37zen.gmf.diagram.MenuitemSubmenu_4002"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	private static ImageRegistry getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
		}
		return imageRegistry;
	}

	/**
	 * @generated
	 */
	private static String getImageRegistryKey(ENamedElement element) {
		return element.getName();
	}

	/**
	 * @generated
	 */
	private static ImageDescriptor getProvidedImageDescriptor(
			ENamedElement element) {
		if (element instanceof EStructuralFeature) {
			EStructuralFeature feature = ((EStructuralFeature) element);
			EClass eContainingClass = feature.getEContainingClass();
			EClassifier eType = feature.getEType();
			if (eContainingClass != null && !eContainingClass.isAbstract()) {
				element = eContainingClass;
			} else if (eType instanceof EClass
					&& !((EClass) eType).isAbstract()) {
				element = eType;
			}
		}
		if (element instanceof EClass) {
			EClass eClass = (EClass) element;
			if (!eClass.isAbstract()) {
				return AwesomeDiagramEditorPlugin.getInstance()
						.getItemImageDescriptor(
								eClass.getEPackage().getEFactoryInstance()
										.create(eClass));
			}
		}
		// TODO : support structural features
		return null;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(ENamedElement element) {
		String key = getImageRegistryKey(element);
		ImageDescriptor imageDescriptor = getImageRegistry().getDescriptor(key);
		if (imageDescriptor == null) {
			imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
		}
		return imageDescriptor;
	}

	/**
	 * @generated
	 */
	public static Image getImage(ENamedElement element) {
		String key = getImageRegistryKey(element);
		Image image = getImageRegistry().get(key);
		if (image == null) {
			ImageDescriptor imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
			image = getImageRegistry().get(key);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImageDescriptor(element);
	}

	/**
	 * @generated
	 */
	public static Image getImage(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImage(element);
	}

	/**
	 * Returns 'type' of the ecore object associated with the hint.
	 * 
	 * @generated
	 */
	public static ENamedElement getElement(IAdaptable hint) {
		Object type = hint.getAdapter(IElementType.class);
		if (elements == null) {
			elements = new IdentityHashMap<IElementType, ENamedElement>();

			elements.put(Awesome_1000, AwesomePackage.eINSTANCE.getAwesome());

			elements.put(Desktop_2001, AwesomePackage.eINSTANCE.getDesktop());

			elements.put(Menu_3001, AwesomePackage.eINSTANCE.getMenu());

			elements.put(Menuitem_3002, AwesomePackage.eINSTANCE.getMenuitem());

			elements.put(TopBox_3003, AwesomePackage.eINSTANCE.getTopBox());

			elements.put(BottomBox_3004,
					AwesomePackage.eINSTANCE.getBottomBox());

			elements.put(LeftBox_3005, AwesomePackage.eINSTANCE.getLeftBox());

			elements.put(RightBox_3006, AwesomePackage.eINSTANCE.getRightBox());

			elements.put(MenuitemSubmenu_4002,
					AwesomePackage.eINSTANCE.getMenuitem_Submenu());
		}
		return (ENamedElement) elements.get(type);
	}

	/**
	 * @generated
	 */
	private static IElementType getElementType(String id) {
		return ElementTypeRegistry.getInstance().getType(id);
	}

	/**
	 * @generated
	 */
	public static boolean isKnownElementType(IElementType elementType) {
		if (KNOWN_ELEMENT_TYPES == null) {
			KNOWN_ELEMENT_TYPES = new HashSet<IElementType>();
			KNOWN_ELEMENT_TYPES.add(Awesome_1000);
			KNOWN_ELEMENT_TYPES.add(Desktop_2001);
			KNOWN_ELEMENT_TYPES.add(Menu_3001);
			KNOWN_ELEMENT_TYPES.add(Menuitem_3002);
			KNOWN_ELEMENT_TYPES.add(TopBox_3003);
			KNOWN_ELEMENT_TYPES.add(BottomBox_3004);
			KNOWN_ELEMENT_TYPES.add(LeftBox_3005);
			KNOWN_ELEMENT_TYPES.add(RightBox_3006);
			KNOWN_ELEMENT_TYPES.add(MenuitemSubmenu_4002);
		}
		return KNOWN_ELEMENT_TYPES.contains(elementType);
	}

	/**
	 * @generated
	 */
	public static IElementType getElementType(int visualID) {
		switch (visualID) {
		case AwesomeEditPart.VISUAL_ID:
			return Awesome_1000;
		case DesktopEditPart.VISUAL_ID:
			return Desktop_2001;
		case MenuEditPart.VISUAL_ID:
			return Menu_3001;
		case MenuitemEditPart.VISUAL_ID:
			return Menuitem_3002;
		case TopBoxEditPart.VISUAL_ID:
			return TopBox_3003;
		case BottomBoxEditPart.VISUAL_ID:
			return BottomBox_3004;
		case LeftBoxEditPart.VISUAL_ID:
			return LeftBox_3005;
		case RightBoxEditPart.VISUAL_ID:
			return RightBox_3006;
		case MenuitemSubmenuEditPart.VISUAL_ID:
			return MenuitemSubmenu_4002;
		}
		return null;
	}

}
